package com.ztest.betsimu;

import com.apop.MainApplication;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Random;

/**
 * @Description:
 * @Author: zjy
 * @Date: 2025/2/18 14:10
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class BetSimulation {

    // 赌卡成功率（例如50%）
    private static final double SUCCESS_RATE = 0.48;

    // 赌卡成本（低价卡和高价卡的成本）
    private static final int LOW_CARD_COST = 1000;
    private static final int HIGH_CARD_COST = 5000;

    // 初始合同费
    private static final int INITIAL_CONTRACT_FEE = 100_0000;

    // 模拟赌卡过程
    public void simulateBet(int numberOfAttempts) {
        Random random = new Random();
        int currentContractFee = INITIAL_CONTRACT_FEE;
        int betAmount = LOW_CARD_COST; // 初始赌注

        System.out.println("环境：初始本金 " + betAmount + " 成功率 " + currentContractFee);

        for (int i = 1; i <= numberOfAttempts; i++) {
            System.out.println("第 " + i + " 次赌卡，赌注: " + betAmount);

            // 模拟赌卡结果
            boolean isSuccess = random.nextDouble() < SUCCESS_RATE;

            if (isSuccess) {
                // 赌卡成功，获得高价卡
                int profit = HIGH_CARD_COST - betAmount;
                currentContractFee += profit;
                System.out.println("赌卡成功！获得合同费: " + profit + "，当前合同费: " + currentContractFee);
                betAmount = LOW_CARD_COST; // 重置赌注
            } else {
                // 赌卡失败，亏损赌注
                currentContractFee -= betAmount;
                System.out.println("赌卡失败！亏损合同费: " + betAmount + "，当前合同费: " + currentContractFee);
                betAmount *= 2; // 倍投法：失败后加倍赌注
            }

            // 如果合同费不足以继续赌卡，结束模拟
            if (currentContractFee < betAmount) {
                System.out.println("合同费不足，无法继续赌卡。");
                break;
            }
        }

        System.out.println("模拟结束，最终合同费: " + currentContractFee);
    }

    @Test
    public void mainTest() {
        int numberOfAttempts = 2000; // 模拟赌卡次数
        simulateBet(numberOfAttempts);
    }


}
